--
-- Created by IntelliJ IDEA.
-- User: galileoliu
-- Date: 2018/4/13
-- Time: 14:32
-- Copyright (c) 2018      Figure Inc.
--

local GrayBattleTrackView = import("..views.GrayBattleTrackView")
local GrayBattleRewardTips = import("..views.GrayBattleRewardTips")

local fileName = string.getModeName(...)
local GrayBattleController = class(fileName, app.mvc.ControllerBase)

function GrayBattleController:ctor(params)
    self.super.ctor(self, params)
end

function GrayBattleController:init()

end

function GrayBattleController:removeListeners()
    self.super.removeListeners(self)
end

function GrayBattleController:listEvent()
    return {
        "EVENT_GRAY_BATTLE_REWARD_NOTIFY",
    }
end

function GrayBattleController:onEvent(eventName, data)
    if eventName == "EVENT_GRAY_BATTLE_REWARD_NOTIFY" then
        local loots = {}
        local souls = data.soul
        local loot
        for _, soul in ipairs(souls) do
            loot = {}
            loot.type = Enums.TeamTarget.ashInvade * 10 + soul
            loot.num = 1
            table.insert(loots, loot)
        end
        loot = {
            type = Enums.PropType.eventPoint,
            num = data.activityNum,
        }
        table.insert(loots, loot)
        DelayStack:pushToFront({name = "dungeonDrop",loots = loots, extra = {type = data.userAddType, num = data.userAddNum}})
    end
end

function GrayBattleController:openView(data)
    if cc.isDead(self._view) then
        self._view = GrayBattleTrackView.new(app.mvc.VIEWTYPE_BLUR, data)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    self._view:show()
end

function GrayBattleController:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end

function GrayBattleController:clearView()
    if self._view then
        self._view = nil
    end
end

local searchMapList =
{
    102,
    103,
    104
}

-- 帮助函数：通知UI关闭
local function sureNotify()
    app:sendMsg("NewTeamMemberController", "closeView")
    app:sendMsg("TeamMatchController", "closeView")
    app:sendMsg("PvpController", "closeView")
    app:sendMsg("NewDungeonTypeController", "closeView")
    app:sendMsg("NewActivityDungeonsController", "closeView")
end

function GrayBattleController:onOperate(op, data)
    if op == "go" then
        local character = FieldManager:getMyCharacter()
        local nearestNpc = NpcManager:getNearestGrayBattleNpc(character:getPositionX())
        if nearestNpc then
            -- 当前地图有灰烬入侵NPC
            character:autoWayFinding(nearestNpc:getOriginalX())
            sureNotify()
        else
            -- 当前地图没有灰烬入侵NPC，搜索其他地图距离传送点最近的灰烬入侵NPC
            local searchMapId
            local nearestNpcData
            for _, mapId in ipairs(searchMapList) do
                if mapId ~= MapManager:getMapId() then
                    searchMapId = mapId
                    nearestNpcData = UD:getGrayBattleNpcDataByMapId(searchMapId)
                    if nearestNpcData ~= nil then
                        break
                    end
                end
            end

            if nearestNpcData then
                FieldManager:getToNpcAutoWayFinding(nearestNpcData.uid, false, true)
                sureNotify()
            else
                --TODO: 所有灰烬入侵NPC已经被打完
                display.pushToast(L("ashes_over"))
            end
        end
--        EM:notify("onTrackingWay",
--            {
--                trackNpcId = npcId,
--                autoStartEvents = false,
--                isTracking = true,
--                isLegionTrack = true,
--            })
    elseif op == "close" then

    elseif op == "closeFinal" then

    elseif op == "gotoTeam" then
        local params = {}
        params.id = Constant.FUNC_TYPE_TEAMMATCHING
        params.mode = "open"
        params.data = {targetId=Enums.TeamTarget.ashInvade}
        UIHandler:handleMessage(params)
    elseif op == "getReward" then
        TaskManager:C2S_TaskGetGrayBattleRewardRequest(function()
            --TODO:
        end)
    elseif op == "showRewardTips" then
        self._rewardTips = GrayBattleRewardTips.create()
        data.x = data.x - 380
        data.y = data.y - 100
        self._rewardTips:setPosition(data)
        self._view:addChild(self._rewardTips)
    elseif op == "hideRewardTips" then
        if self._rewardTips ~= nil then
            self._rewardTips:removeFromParent()
        end
    end
end

return GrayBattleController
